﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ERPStudio;
using System.Data.SqlClient;

namespace ESRP
{
    public partial class SolduriInitialeJurnaleFrm : FrmRezumat
    {
        private ESRPDataSet.ConturiRow cRow = null;
        private Boolean SolduriPrecedenteVizibile = false;
        public SolduriInitialeJurnaleFrm(ModuleClass mc,ESRPDataSet.ConturiRow crow,Boolean si): base(mc)
        {
            InitializeComponent();
            cRow = crow;
            Text = Text + " pentru contul: " + cRow.Cont;
            eSRPDataSet.JurnaleSolduriInitiale.CodUnitateColumn.DefaultValue = ModuleInfo.CodUnitate;
            eSRPDataSet.JurnaleSolduriInitiale.ContColumn.DefaultValue = cRow.Cont;
            eSRPDataSet.JurnaleSolduriInitiale.DebitPrecedentColumn.DefaultValue = 0;
            eSRPDataSet.JurnaleSolduriInitiale.CreditPrecedentColumn.DefaultValue = 0;
            eSRPDataSet.JurnaleSolduriInitiale.DebitInitialColumn.DefaultValue = 0;
            eSRPDataSet.JurnaleSolduriInitiale.CreditInitalColumn.DefaultValue = 0;

       
            SolduriPrecedenteVizibile = si;
            debitPrecedentColumn.Visible = SolduriPrecedenteVizibile;
            creditPrecedentColumn.Visible = SolduriPrecedenteVizibile;
            
            if (cRow.Tip == "A")
            {
                debitInitialColumn.ReadOnly = false;
                creditInitialColumn.ReadOnly = true;
                creditInitialColumn.DefaultCellStyle.BackColor = Color.LightGray;
            }
            if (cRow.Tip == "P")
            {
                debitInitialColumn.ReadOnly = true;
                creditInitialColumn.ReadOnly = false;
                debitInitialColumn.DefaultCellStyle.BackColor = Color.LightGray;
            }
        }

        private void SolduriInitialeJurnaleFrm_Load(object sender, EventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString);
            jurnaleTableAdapter.Connection = con;
            jurnaleTableAdapter.Fill(eSRPDataSet.Jurnale, ModuleInfo.CodUnitate);

            if (jurnaleBindingSource.Count == 0)
            {
                MessageBoxFrm mbt = new MessageBoxFrm("Eroare", "Va rugam sa definiti jurnalele inainte de a defalca soldurile initiale!", String.Empty, MessageBoxFrm.ImaginiMB.StopIcon, null);
                mbt.AddButton("Ieşire", 70, mbt.MessageBoxFrm_Click_Yes);
                mbt.ShowDialog();
                Close();
                return;
            }
            if(jurnaleBindingSource.Current!=null)
            {
                ESRPDataSet.JurnaleRow jRow = ((DataRowView)jurnaleBindingSource.Current).Row as ESRPDataSet.JurnaleRow;
                if (jRow != null)
                {
                    eSRPDataSet.JurnaleSolduriInitiale.IDJurnalColumn.DefaultValue = jRow.IDJurnal;
                }
            }


            this.jurnaleSolduriInitialeTableAdapter.Connection = con;
            this.jurnaleSolduriInitialeTableAdapter.FillByCont(this.eSRPDataSet.JurnaleSolduriInitiale, cRow.Cont,ModuleInfo.CodUnitate);
            InchideConexiune(con);

            AddBindingSource(jurnaleSolduriInitialeBindingSource);
            AddTableAdaptor(jurnaleSolduriInitialeTableAdapter.Adapter);
        }

        override public void Salvare(object sender, EventArgs args)
        {
            base.Salvare(this, args);
            if (base.NoRecUpdated != 0)
            {
            }
        }

        private void SolduriInitialeJurnaleFrm_FormClosed(object sender, FormClosedEventArgs e)
        {
            SqlConnection con = DeschideConexiune(workConnString,120);
			SqlCommand cmd = new SqlCommand("balcon_StergeSolduriInitialeCapitoleZero", con);
			cmd.CommandType = CommandType.StoredProcedure;
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                MessageBoxFrm mbt = new MessageBoxFrm("Eroare", ex.Message, String.Empty, MessageBoxFrm.ImaginiMB.StopIcon, null);
                mbt.AddButton("Ieşire", 70, mbt.MessageBoxFrm_Click_Yes);
                mbt.ShowDialog();
            }
            finally
            {
                InchideConexiune(con);
            }
        }

    }
}
